package com.sh.data.engine.web.webapi.controller.api;

import com.sh.data.engine.application.model.dto.api.*;
import com.sh.data.engine.application.model.dto.common.DirectoryResponse;
import com.sh.data.engine.application.model.dto.integration.DataSourceDTO;
import com.sh.data.engine.application.model.request.api.DataApiCallRecordRequest;
import com.sh.data.engine.application.service.api.DataApiOverviewApplicationService;
import com.sh.data.engine.domain.api.service.DataApiCallRecordService;
import com.sh.data.engine.domain.api.service.DataApiOverviewService;
import com.sh.data.engine.domain.api.service.DataApiService;
import com.sh.data.engine.domain.base.model.PageResult;
import com.sh.data.engine.domain.common.service.DirectoryService;
import com.sh.data.engine.domain.integration.datasource.service.DataSourceService;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author : fuchen
 * @date : 2022/2/10 13:57
 */
@Validated
@RestController
@RequestMapping("/dataApiOverview")
@Tag(name = "数据服务-概览", description = "数据服务-概览API")
@Slf4j
public class DataApiOverviewController {

    @Autowired
    private DataApiOverviewService dataApiOverviewService;

    @Autowired
    private DataApiCallRecordService dataApiCallRecordService;

    @Autowired
    private DataApiService dataApiService;

    @Autowired
    private DataSourceService dataSourceService;

    @Autowired
    private DirectoryService directoryService;

    @Autowired
    private DataApiOverviewApplicationService dataApiOverviewApplicationService;

    @GetMapping("/getLocalUserAuth")
////    @AuthorityAnnotation(menuId = MenuEnumNew.GL2, readWriteStatus = 0)
    @Operation(summary = "获取本地用户权限")
    public RestResponseEntity<Boolean> getLocalUserAuth() {
        var result = dataApiOverviewApplicationService.getLocalUserAuth();
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getOverviewSummary")
    @Operation(summary = "概览-头")
//    @AuthorityAnnotation(menuId = MenuEnumNew.GL2, readWriteStatus = 0)
    public RestResponseEntity<DataApiOverviewSummaryResponse> getOverviewSummary(
        @Parameter(description = "类型 0-全局概览 1-我的概览", schema = @Schema(allowableValues = {"0", "1"}), required = true) @RequestParam
        Integer type) {
        var result = dataApiOverviewApplicationService.getOverviewSummary(type);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getApprovalSummary")
    @Operation(summary = "审批统计")
//    @AuthorityAnnotation(menuId = MenuEnumNew.GL2, readWriteStatus = 0)
    public RestResponseEntity<DataApiApprovalSummaryResponse> getApprovalSummary(
        @Parameter(description = "类型 0-全局概览 1-我的概览", schema = @Schema(allowableValues = {"0", "1"}), required = true) @RequestParam
        Integer type) {
        var result = dataApiOverviewApplicationService.getApprovalSummary(type);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getReqTrendSummary")
    @Operation(summary = "调用次数趋势统计")
//    @AuthorityAnnotation(menuId = MenuEnumNew.GL2, readWriteStatus = 0)
    public RestResponseEntity<List<DataApiReqTrendSummaryResponse>> getReqTrendSummary(
        @Parameter(description = "类型 0-全局概览 1-我的概览", schema = @Schema(allowableValues = {"0", "1"}), required = true) @RequestParam
        Integer type,
        @Parameter(description = "开始日期", schema = @Schema(format = "yyyy-MM-dd"), required = true) @RequestParam
        String startDate,
        @Parameter(description = "结束日期", schema = @Schema(format = "yyyy-MM-dd"), required = true) @RequestParam
        String endDate) {
        var result = dataApiOverviewApplicationService.getReqTrendSummary(type, startDate, endDate);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getTopUserReqSummary")
    @Operation(summary = "调用用户TOP")
//    @AuthorityAnnotation(menuId = MenuEnumNew.GL2, readWriteStatus = 0)
    public RestResponseEntity<Map<String, Object>> getTopUserReqSummary() {
        var result = dataApiOverviewApplicationService.getTopUserReqSummary();
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getTopApiReqSummary")
    @Operation(summary = "调用量TOP")
//    @AuthorityAnnotation(menuId = MenuEnumNew.GL2, readWriteStatus = 0)
    public RestResponseEntity<Map<String, Object>> getTopApiReqSummary(
        @Parameter(description = "类型 0-全局概览 1-我的概览", schema = @Schema(allowableValues = {"0", "1"}), required = true) @RequestParam
        Integer type) {
        var result = dataApiOverviewApplicationService.getTopApiReqSummary(type);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getTopApiErrReqSummary")
    @Operation(summary = "失败次数TOP")
//    @AuthorityAnnotation(menuId = MenuEnumNew.GL2, readWriteStatus = 0)
    public RestResponseEntity<Map<String, Object>> getTopApiErrReqSummary(
        @Parameter(description = "类型 0-全局概览 1-我的概览", schema = @Schema(allowableValues = {"0", "1"}), required = true) @RequestParam
        Integer type) {
        var result = dataApiOverviewApplicationService.getTopApiErrReqSummary(type);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getErrDistributionSummary")
    @Operation(summary = "错误类型分布统计")
//    @AuthorityAnnotation(menuId = MenuEnumNew.GL2, readWriteStatus = 0)
    public RestResponseEntity<Map<String, Object>> getErrDistributionSummary(
        @Parameter(description = "类型 0-全局概览 1-我的概览", schema = @Schema(allowableValues = {"0", "1"}), required = true) @RequestParam
        Integer type) {
        var result = dataApiOverviewApplicationService.getErrDistributionSummary(type);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/listDataApiRecord")
    @Operation(summary = "数据服务统计")
//    @AuthorityAnnotation(menuId = MenuEnumNew.GL2, readWriteStatus = 0)
    public RestResponseEntity<PageResult<DataApiCallRecordResponse>> listDataApiRecord(
        DataApiCallRecordRequest recordRequest) {
        var result = dataApiOverviewApplicationService.listDataApiRecord(recordRequest);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/listDsName")
    @Operation(summary = "数据源列表")
//    @AuthorityAnnotation(menuId = MenuEnumNew.GL2, readWriteStatus = 0)
    public RestResponseEntity<List<DataSourceDTO>> listDsName() {
        var result = dataApiOverviewApplicationService.listDsName();
        return RestResponseEntity.success(result);
    }

    @GetMapping("/listDirName")
    @Operation(summary = "目录列表")
//    @AuthorityAnnotation(menuId = MenuEnumNew.GL2, readWriteStatus = 0)
    public RestResponseEntity<List<DirectoryResponse>> listDirName() {
        var result = dataApiOverviewApplicationService.listDirName();
        return RestResponseEntity.success(result);
    }

    @PostMapping("/batchIgnored")
    @Operation(summary = "数据服务统计-归档")
//    @AuthorityAnnotation(menuId = MenuEnumNew.GL2, readWriteStatus = 0)
    public RestResponseEntity<Boolean> batchIgnored(String ids) {
        var result = dataApiOverviewApplicationService.batchIgnored(ids);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/log")
    @Operation(summary = "查看日志")
//    @AuthorityAnnotation(menuId = MenuEnumNew.GL2, readWriteStatus = 0)
    public RestResponseEntity<DataApiLogResponse> getRequestLog(
        @Parameter(description = "请求id", required = true) @RequestParam("logId") Long logId,
        @Parameter(description = "偏移量", required = true) @RequestParam("offset") Integer offset) {
        var result = dataApiOverviewApplicationService.getRequestLog(logId, offset);
        return RestResponseEntity.success(result);
    }


}
